//
//  MyPointsViewController.m
//  OrderMeal
//
//  Created by 周浩 on 14-3-9.
//  Copyright (c) 2014年 周浩. All rights reserved.
//

#import "MyPointsViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "pointsTableViewCell.h"
#import "MBProgressHUD.h"
@interface MyPointsViewController ()
{
    NSString *_customerID; //会员id
    MBProgressHUD *hud ;
}

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(strong,nonatomic)NSArray *pointsData;
-(void)requestWithCustomerID:(NSString *)customerId;
@property (strong,nonatomic)UIRefreshControl *refreshControl;
@end

@implementation MyPointsViewController
@synthesize pointsData = _pointsData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"等待加载数据";
    [self.view addSubview:hud];
    
    NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
    _customerID=[userDefaults objectForKey:@"memberID"];
    NSLog(@"customId--->%@",[_customerID class]);
    [self requestWithCustomerID:_customerID];
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc]init];
    refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];
    [refreshControl addTarget:self action:@selector(refreshTableView) forControlEvents:UIControlEventValueChanged];
    self.refreshControl = refreshControl;
}


//刷新tableView
-(void)refreshTableView{
    self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"加载中"];
    [self requestWithCustomerID:_customerID];
    [self performSelector:@selector(afterRefresh:) withObject:nil afterDelay:0.1];
    
}

-(void)afterRefresh:(id)obj{
    [self.refreshControl endRefreshing];
    self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];
}


-(void)requestWithCustomerID:(NSString *)customerId
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    NSDictionary *parameters=@{
                               @"customerId" :customerId
                              };
    [manager GET:points_url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
     self.pointsData=responseObject[@"retData"];
        [self.tableView reloadData];
        [hud removeFromSuperview];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    
}];
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.pointsData == nil) {
        return 0;
    }
    return [self.pointsData count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    pointsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pointsCell"];
    NSUInteger row = [indexPath row];
    NSDictionary *pointsDic = [self.pointsData objectAtIndex:row];
    cell.pointsLabel.text = [NSString stringWithFormat:@"%@积分",pointsDic[@"num"]];
    cell.dateLabel.text = pointsDic[@"createTime"];
    cell.descriptionLabel.text = pointsDic[@"description"];
    return cell;
}

- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
